package com.jiamao.goplay.ui.login.vm;

import android.app.Application;
import android.text.TextUtils;
import android.util.Log;

import com.jiamao.goplay.bean.BaseRep;
import com.jiamao.goplay.bean.res.ResImage;
import com.jiamao.goplay.bean.res.ResLogin;
import com.jiamao.goplay.config.Config;
import com.jiamao.goplay.data.DataRepository;
import com.jiamao.goplay.ui.login.AddBasicInfoFragment;
import com.jiamao.goplay.ui.login.LoginPhoneFragment;
import com.jiamao.goplay.ui.main.MainActivity;
import com.netease.nim.uikit.api.NimUIKit;
import com.netease.nim.uikit.common.util.log.LogUtil;
import com.netease.nim.uikit.common.util.string.MD5;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.auth.AuthService;
import com.netease.nimlib.sdk.auth.LoginInfo;
import com.tamsiree.rxkit.view.RxToast;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.io.File;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.SPUtils;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class LoginWeChatModel extends BaseViewModel<DataRepository> {

    public ObservableField<Boolean> isYes = new ObservableField<>(false);


    public LoginWeChatModel(@NonNull Application application, DataRepository model) {
        super(application, model);
    }


    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        //密码开关观察者
        public SingleLiveEvent<Boolean> wechatLogin = new SingleLiveEvent<>();




    }

    public BindingCommand loginPhoneOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            uc.wechatLogin.setValue(true);

        }
    });
    public BindingCommand weChatOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {


            uc.wechatLogin.setValue(false);




        }
    });



    /**
     * 网络模拟一个登陆操作
     **/
    public void loginWeChat(String UniqueId,String AppOpenid) {

        String md5 = MD5.getStringMD5(AppOpenid+UniqueId+Config.ApiAuthKey);
        model.getApi().loginWeChat(UniqueId,AppOpenid,md5)

                .compose(RxUtils.bindToLifecycle(getLifecycleProvider())) // 请求与View周期同步
                .compose(RxUtils.schedulersTransformer())  // 线程调度
                .compose(RxUtils.exceptionTransformer())   // 网络错误的异常转换
                .doOnSubscribe(this) //请求与ViewModel周期同步
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        showDialog();
                        // dialog.show();
                    }
                })
                .subscribe(new DisposableObserver<BaseRep<ResLogin>>() {

                    @Override
                    public void onError(Throwable throwable) {
                        //关闭对话框
                        dismissDialog();
                        //请求刷新完成收回
                        RxToast.showToastShort(throwable.getMessage());

                    }

                    @Override
                    public void onComplete() {
                        dismissDialog();
                    }

                    @Override
                    public void onNext(BaseRep<ResLogin> o)   {
                        dismissDialog();
                        if (o.isOk()){
                            model.getmLocalDataSource().saveImToken(o.getData().getIMToken());
                            model.getmLocalDataSource().saveToken(o.getData().getToken());
                            model.getmLocalDataSource().saveUid(o.getData().getUid());
                            if(o.getData().getIsPerfect() == 0){
                                KLog.d("HTML","进入资料编辑页面");
                                startContainerActivity(AddBasicInfoFragment.class.getCanonicalName());
                                finish();
                            }
                            else {
                                KLog.d("HTML","进入首页等公路");
                              //  model.saveUserName(phone);
                                Log.d("HTML","UID:"+model.getmLocalDataSource().getUid()+"  token:"+ model.getmLocalDataSource().getIMToken());
                                NimUIKit.login(new LoginInfo(model.getmLocalDataSource().getUid()+"", model.getmLocalDataSource().getIMToken()), new RequestCallback<LoginInfo>() {
                                    @Override
                                    public void onSuccess(LoginInfo param) {

                                        SPUtils.getInstance().put(Config.IS_LOGIN,true);
                                        startActivity(MainActivity.class);
                                        finish();
                                    }

                                    @Override
                                    public void onFailed(int code) {
                                        RxToast.showToastShort("IM服务器登陆失败"+code);
//
                                    }

                                    @Override
                                    public void onException(Throwable exception) {
                                        RxToast.showToastShort("IM 登录异常"+exception.getMessage());
                                    }
                                });
//
                            }
                        }else {
                            RxToast.showToastShort(o.getMsg());
                        }
                    }
                });

    }








}
